SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION [dbo].[FN_TDCUOT_SOLI_Q01]
/*------------------------------------------------*/
/*---- Empresa		   : OFISIS S.A		----*/
/*---- Cliente		   : OFISIS S.A		----*/
/*---- Sistema		   : Ofiteso			----*/
/*---- Modulo		   : Empresa			----*/
/*---- Programa	   : Verifica Descuento Cuotas ---*/
/*---- Nombre SP	   : FN_TDCUOT_SOLI_Q01	----*/
/*---- Desarrollado por: Carlos Marquez Escobar----*/
/*---- Fecha Creacion  : 04/05/2004		----*/
/*---- Base de Datos   : SQL				----*/
/*---- Version		   : 2000				----*/
/*---- Invoca a SP 	   :					----*/
/*------------------------------------------------*/
/*-------------------------------------------------*/
/*---- Comentarios         : 				----*/
/*---- Drop FUNCTION  FN_TDCUOT_SOLI_Q01    ----*/
/*---- Grant ALL on FN_TDCUOT_SOLI_Q01 to public----*/
/*---- 								----*/
/*------------------------------------------------*/
/*---- Declare  @SI_EMININO TD_VC_001
       Select @SI_EMININO = (SELECT OFITESO.dbo.FN_TDCUOT_SOLI_Q01('01', '00000000001', 'FAC', '0001-0000000001') )
       Select @SI_EMININO							---*/
/*---- 									----*/
/*--------------------------------------------------------------------------*/
(	@CSCO_EMPR			TD_VC_002,
	@CSCO_ENTI			TD_VC_020,
	@CSTI_DOCU_ENTI		TD_VC_003,
	@CSNU_DOCU_ENTI		TD_VC_020 ) RETURNS TD_VC_001

As
Begin
 Declare 


	@VSNU_CORR_SOLI		TD_VC_008,
	@VSCO_COND_PAGO		TD_VC_003,
	@VSCO_TRAB			TD_VC_020,
	@OSST_DESC_COUT		TD_VC_001


    Select @OSST_DESC_COUT = 'S'

      -- Obtiene los Datos del Documentos Solicitud para la inserción --
	Select 	@VSCO_COND_PAGO	=	CO_COND_PAGO
	From TMDOCU_CLIE
	Where
		CO_EMPR = @CSCO_EMPR  And
		CO_CLIE = @CSCO_ENTI  And
		CO_TIPO_DOCU = @CSTI_DOCU_ENTI  And
		NU_DOCU_CLIE = @CSNU_DOCU_ENTI  And
		CO_ESTA_DOCU != 'ANU'


	If @VSCO_COND_PAGO = 'C01'
	Begin

		Select @VSNU_CORR_SOLI = t2.NU_RECI_COBR
		From TCMOVI_CAJA t1, TDMOVI_CAJA t2
		Where  t1.CO_EMPR = t2.CO_EMPR And
				t1.CO_CAJA = t2.CO_CAJA And
				t1.AA_CAJA = t2.AA_CAJA And
				t1.MM_CAJA = t2.MM_CAJA And
				t1.TI_MOVI = t2.TI_MOVI And
				t1.NU_COMP_CAJA = t2.NU_COMP_CAJA And
				t1.TI_SITU != 'ANU' And
				t2.TI_ENTI = 'C' And
				t2.CO_ENTI = @CSCO_ENTI And
				t2.TI_DOCU_ENTI = @CSTI_DOCU_ENTI And
				t2.NU_DOCU_ENTI = @CSNU_DOCU_ENTI

		If Len(RTrim(Ltrim(IsNull(@VSNU_CORR_SOLI,'')))) > 0
		Begin 

		      -- Obtiene el Codigo de Trabajador en Base al Codigo de Cliente --		
			Select @VSCO_TRAB = '0' + SubString(@CSCO_ENTI, 2, Len(@CSCO_ENTI) - 1 )

			If Exists ( Select 	NU_CORR_SOLI
					From TDCUOT_SOLI			
					Where	CO_EMPR = @CSCO_EMPR And
					CO_TRAB = @CSCO_ENTI And
					NU_CORR_SOLI = @VSNU_CORR_SOLI And TI_SITU = 'DES')	----situación 'DES'

				    Select @OSST_DESC_COUT = 'N'


		End

	End


    Return (@OSST_DESC_COUT)
    

End

/************************  f i n  **********************************/

GO
